<template>
	<gui-page ref="guiPage" :customHeader="true">
		<!-- 导航 -->
		<template v-slot:gHeader>
			<nav-bar-custom left-icon="left" :leftType="'1'" @clickLeftBack="clickLeftBack"
				:title="$t('whether.tonBagFeeding')" scanType="1" @clickRight="clickRight"></nav-bar-custom>
		</template>
		<template v-slot:gBody>
			<view class="barcodeSplitting" :style="'height: calc(100vh - ' + navHeight + 'px);'">
				<view class="custom-form-card">
					<view class="custom-card-title">
						<text class="title-line"></text>
						{{ $t('distributedEnter.barcodeScan') }}
					</view>
					<view class="custom-card-itemTwo" style="padding: 20rpx 0;">
						<text class="item-info-title"><text class="required-color">*</text>{{ $t('taskList.tankNumber')
						}}：</text>
						<superweiCombox style="text-align: right;" :candidates="data.tankList"
							:placeholder="$t('distributedOut.placeholder')" :isJSON="true" keyName="text"
							v-model="data.tankNname" @select="getRuleClick"></superweiCombox>
					</view>
					<view class="custom-card-itemTwo" style="padding: 20rpx 0">
						<text class="item-info-title">
							<text class="required-color">*</text>{{ $t('Common.scanningBarCode') }}：
						</text>
						<uni-easyinput class="uni-mt-5" suffixIcon="scan" v-model="data.barcode"
							:placeholder="$t('materials.scan')" @confirm="confirmLocation"
							@iconClick="iconClickLocation"></uni-easyinput>
					</view>
				</view>

				<!-- 卡片模块 -->
				<scroll-view :show-scrollbar="false" :scroll-y="true" class="scroll element-auto">
					<view style="height: 15rpx"></view>
					<view v-if="data.barcodeInfo" class="card-boxCustom">
						<view class="card-body">
							<view class="custom-card-title">
								<text class="title-line"></text>
								{{ $t('pageTranslation.plsScanBarcodeInfo') }}
							</view>
							<view class="custom-card-itemOne">
								<text class="body-item-title">{{ $t('distributedEnter.materialCoded') }}：</text>
								{{ data.barcodeInfo.barcode }}
							</view>
							<view class="custom-card-itemOne">
								<text class="body-item-title">{{ $t('distributedEnter.materialCode') }}：</text>
								{{ data.barcodeInfo.materialCode }}
							</view>
							<view class="custom-card-itemOne">
								<text class="body-item-title">{{ $t('distributedEnter.materialName') }}：</text>
								{{ data.barcodeInfo.materialName }}
							</view>
							<view class="custom-card-itemOne">
								<text class="body-item-title">{{ $t('distributedEnter.materialSpec') }}：</text>
								{{ data.barcodeInfo.materialSpec }}
							</view>
							<view class="custom-card-itemOne">
								<view class="card-item-50">
									<text class="body-item-title">{{ $t('distributedEnter.uom') }}：</text>
									{{ data.barcodeInfo.unit }}
								</view>
								<view class="card-item-50">
									<text class="body-item-title">{{ $t('distributedEnter.qty') }}：</text>
									{{ data.barcodeInfo.currentQty }}
								</view>
							</view>
							<view class="custom-card-itemOne">
								<text class="body-item-title">{{ $t('translationImage.warehouseSubordinate') }}：</text>
								{{ data.barcodeInfo.warehouseName }}
							</view>
							<view class="custom-card-itemOne">
								<text class="body-item-title">{{ $t('multilingual.locationBelonged') }}：</text>
								{{ data.barcodeInfo.locationCode }}
							</view>
							<view class="custom-card-itemOne">
								<text class="body-item-title">{{ $t('slottedAsh.one-checkState') }}：</text>
								{{ data.barcodeInfo.qcStatus == null ? 'waitCheck' : data.barcodeInfo.qcStatus }}
							</view>
							<view class="custom-card-itemOne">
								<text class="body-item-title">{{ $t('slottedAsh.secondaryInspectionState') }}：</text>
								{{ data.barcodeInfo.secondQcStatus == null ? 'waitCheck' :
									data.barcodeInfo.secondQcStatus }}
							</view>

							<view class="custom-card-itemOne">
								<text class="body-item-title">{{ $t('taskList.outputLine') }}：</text>
								{{ data.barcodeInfo.productionLineCode }}
							</view>
							<view class="custom-card-itemOne">
								<text class="body-item-title">{{ $t('slottedAsh.productionTimeEnd') }}：</text>
								{{ data.barcodeInfo.productionTime }}
							</view>
							<view class="custom-card-itemOne">
								<text class="body-item-title">{{ $t('fixedAssets.distributedOutNo') }}：</text>
								{{ data.barcodeInfo.stepoutDoc }}
							</view>
							<view class="custom-card-itemOne">
								<text class="body-item-title">{{ $t('fixedAssets.stockbatch') }}：</text>
								{{ data.barcodeInfo.stockBatch }}
							</view>
							<view class="custom-card-itemOne">
								<text class="body-item-title">{{ $t('pageTranslation.barcodeStatus') }}：</text>
								{{ data.barcodeInfo.realBarCodeStatus }}
							</view>
						</view>
					</view>
				</scroll-view>

				<!-- 底部确定按钮 -->
				<view class="page-bottom-box">
					<view class="page-bottomBtn bottomBtn-bgcolor-green" @click="confirmFn">{{ $t('calendar.submit') }}
					</view>
				</view>
			</view>
		</template>
	</gui-page>
</template>

<script setup>
import { navigate, navigateBack } from '@/utils/routerSkip.js';
import pdaScan from '@/hooks/pdaScan.js';
import { useStore } from 'vuex';
import { DEFAULT_WAREHOUSECODE, DEFAULT_WAREHOUSEID, OPERATION_COMMIT, OPERATION_SCAN } from '@/constant/index.js';
import { getDataDictionaryFn } from '@/utils/index.js';
import storageBin from '@/components/storageBin.vue';
import comboGrid from '@/components/comboGrid.vue';
import superweiCombox from '@/components/superweiCombox.vue';
import { onShow } from '@dcloudio/uni-app';
import { basPdaOperationLogFn } from '@/utils/index.js';
import { barcodeManagementScan, pvcTonInput, fsCardTonInput } from '@/api/api.js';
import { ref, reactive, onMounted, watch, computed, toRaw } from 'vue';
import { onBackPress } from '@dcloudio/uni-app';

//JS国际化引入
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

components: {
	storageBin,superweiCombox, comboGrid;
}
const storageBinRef = ref(null);
const comboGridRef = ref(null);
let store = useStore();
let inputDialog = ref(null);

const navHeight = computed(() => {
	return store.state.navHeight;
});
const { scan } = pdaScan();

const data = reactive({
	tankList:[{
		text:'PVC5-1  --  '+t('whether.typeTank51'),
		value:'PVC5-1'
	},{
		text:'PVC5-2  --  '+t('whether.typeTank52'),
		value:'PVC5-2'
	},{
		text:'PVC7-1  --  '+t('whether.typeTank71'),
		value:'PVC7-1'
	},{
		text:'PVC7-2  --  '+t('whether.typeTank72'),
		value:'PVC7-2'
	}],
	tankNname:'',
	tankNo:'',
	scanInfo: undefined,
	barcodeInfo: null,
	barcode: ''
});

onMounted(() => {
	uni.$on('scancodedate', (datas) => {
		data.barcode = datas.code;
		getCode(datas.code, 'pda');
	});
});

data.barcodeList = computed(() => {
	return store.state.tonBagFeeding;
});

// 下拉选择
const getRuleClick = (x) => {
	data.tankNo = x.value
	data.tankNname = x.text
}

// 用户调起相机扫码
const iconClickLocation = async (type) => {
	if (type === 'suffix') {
		let code = await scan();
		getCode(code, 'scan');
	}
};
// 用户点击键盘上搜索
const confirmLocation = () => {
	getCode(data.barcode, 'keydown');
};

const getCode = (code, type) => {
	if(!data.tankNname){
		uni.showToast({ title: t('whether.selectTankNumber'), icon: 'none' });
		return
	}
	if (!code) {
		uni.showToast({ title: t('stockManage.pleaseScanCode'), icon: 'none' });
		return;
	}
	let parmes = {
		current: 1,
		size: 99999,
		materialCode: null,
		materialName: null,
		locationCode: null,
		barcode: code,
		barcodeTypeCode: null
	};
	barcodeManagementScan(parmes).then((res) => {
		try {
			let inputParameter = JSON.stringify(parmes);
			let returnParameter = JSON.stringify(res);
			let operationPicture = '吨包投料扫描条码';
			let callInterface = res.rquestUrl;
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: res.code, //返回状态
				errorMessage: res.msg, //错误信息
				isPass: res.code == 0 ? 'Y' : 'N', //是否成功 Y N
				operationType: OPERATION_SCAN,
				module: 'WMS',
				source: 'PDA'
			};
			basPdaOperationLogFn(opertionParmes);
		} catch (error) { }
		console.log(res);
		if (res.code == 0) {
			if (res.data.barcodeStatus != 'inStock') {
				uni.showToast({
					title: t('whether.barcodeIsNot'),
					duration: 3000,
					mask: true,
					icon: 'error'
				});

				return;
			} else if (res.data.warehouseCode != 'YH') {
				uni.showToast({
					title: t('whether.premixedWarehouse'),
					duration: 3000,
					mask: true,
					icon: 'error'
				});

				return;
			} else if (res.data.materialCode != '11PVMF01' && res.data.materialCode != '11PVMF02') {
				uni.showToast({
					title: t('whether.PVCResinCan'),
					duration: 3000,
					mask: true,
					icon: 'error'
				});

				return;
			}

			data.barcodeInfo = res.data;
			data.barcodeInfo.tankNo =data.tankNo
			store.commit('addTonBagFeeding', data.barcodeInfo);
		} else {
			data.barcodeInfo = undefined;
			uni.showToast({ title: res.msg, icon: 'none' });
		}
	});
};

// 提交
const confirmFn = () => {
	if (!data.barcodeList.length) {
		uni.showToast({ title: t('stockManage.pleaseScanCode'), icon: 'none' });
		return;
	}

	var pvcBarcodes = data.barcodeList.filter((e) => e.materialCode == '11PVMF01' || e.materialCode == '11PVMF02');

	// 添加加载框
	uni.showLoading({
		title: 'loading',
		mask: true
	});

	if (pvcBarcodes.length) {
		//选择了PVC

		var parmesPvc = {};
		var lstBarcode = [];

		pvcBarcodes.forEach((e) => {
			lstBarcode.push({ barcode: e.barcode, materialCode: e.materialCode, currentQty: e.currentQty,tankCode:e.tankNo });
		});

		parmesPvc.lstBarcode = lstBarcode;

		pvcTonInput(parmesPvc).then((res) => {
			try {
				let inputParameter = JSON.stringify(parmes);
				let returnParameter = JSON.stringify(res);
				let operationPicture = '吨包投料';
				let callInterface = res.rquestUrl;
				let opertionParmes = {
					inputParameter,
					returnParameter,
					operationPicture,
					callInterface,
					responseCode: res.code, //返回状态
					errorMessage: res.msg, //错误信息
					isPass: res.code == 200 ? 'Y' : 'N', //是否成功 Y N
					operationType: OPERATION_COMMIT,
					module: 'WMS',
					source: 'PDA'
				};
				basPdaOperationLogFn(opertionParmes);
			} catch (error) { }
			if (res.ok == true) {
				uni.showToast({ title: t('smashRemoval.submitSuccessfully'), icon: 'none' });
				data.barcodeInfo = null;
				store.commit('clearTonBagFeeding');
			} else {
				uni.showToast({ title: res.msg, icon: 'none' });
			}
		});
	}
};

//扫描纪录
const clickRight = () => {
	navigate({
		url: '/pages/aniseedIn/tonBagFeedingRecord'
	});
};

//导航栏左侧返回
const clickLeftBack = () => {
	if (!data.barcodeList.length) {
		uni.navigateBack({
			delta: 1
		});
		return;
	}
	uni.showModal({
		title: t('smashRemoval.warmPrompt'),
		content: t('materials.clearedIfReturned'),
		success: function (res) {
			if (res.confirm) {
				store.commit('clearTonBagFeeding');
				uni.navigateBack({
					delta: 1
				});
			} else if (res.cancel) {
				console.log('用户点击取消');
			}
		}
	});
};

onBackPress((options) => {
	if (options.from == 'backbutton') {
		if (!data.barcodeList.length) {
			uni.navigateBack({
				delta: 1
			});
			return true;
		}
		uni.showModal({
			title: t('smashRemoval.warmPrompt'),
			content: t('materials.unsubmittedDataIn'),
			success: function (res) {
				if (res.confirm) {
					store.commit('clearTonBagFeeding');
					uni.navigateBack({
						delta: 1
					});
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
		return true;
	}
});
</script>

<style lang="scss" scoped>
.barcodeSplitting {
	display: flex;
	flex-direction: column;
	height: calc(100vh - 88rpx);
	// height: 100vh;
}

.distributed-head {
	border-bottom-left-radius: 16rpx;
	border-bottom-right-radius: 16rpx;
}

.scroll {
	flex: 1;
	height: 2rpx;
}

.page-bottom-box {
	position: absolute;
	left: 0px;
	bottom: 0px;
	width: 100%;
}
</style>
